<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <style>
        .el-page-header__content{
            color: white;
            font-weight: bold;
        }
    </style>
</head>
<body>
<div id="app">
    <el-page-header style="background-color: #0095d7;
        color: white;line-height: 60px" @back="goBack()" content="添加轮播图">
    </el-page-header>
    <img :src="oldUrl" v-if="url==''" width="200" alt="">
    <el-upload
            action="/upload"
            name="pic"
            limit="1"
            list-type="picture-card"
            :on-success="handleSuccess"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove">
        <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
    <el-button type="success" @click="update()">修改轮播图</el-button>
</div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
   let v = new Vue({
        el: '#app',
        data: function() {
            return{
                dialogImageUrl: '',
                dialogVisible: false,
                url:"",
                oldUrl:""
            }
        },
       methods: {
           goBack(){
                history.back();
           },
           handleRemove(file, fileList) {
               console.log(file, fileList);
               //发出删除图片的请求
               axios.get("/remove?url="+v.url).then(function () {
                   v.$message("服务器图片删除完成！");
                   v.url="";
               })
           },
           handlePictureCardPreview(file) {
               this.dialogImageUrl = file.url;
               this.dialogVisible = true;
           },
           handleSuccess(response,file,fileList){
               //response代表服务器响应的图片路径
               v.url = response;
           },
           update(){
               if (v.url==""){
                   v.$message.error("请选择上传的图片!");
                   return;
               }
               //发出修改轮播图的请求
               let bannerId = location.search.split("&")[1].split("=")[1];
               //创建保存轮播图信息的对象
               let b = {id:bannerId,url:v.url};
               //发出修改轮播图的请求
               axios.post("/banner/update",b).then(function () {
                   location.href="/admin.html";
               })
           }
       },
       created:function () {
           //created方法是vue对象初始化过程中的方法,并没有初始化完成
           //得到地址栏中从上个页面传递过来的图片路径
           this.oldUrl = location.search.split("&")[0].split("=")[1];
       }
   })
</script>
</html>